<?php

class RobotController extends Zend_Controller_Action
{
    public function init()
    {

        $this->_helper->layout()->disableLayout();

    }


    public function robotAction()
    {

        $news = new \Managers\Pages();
        $news = $news->getPages();
        if (isset(Zend_Registry::get('config')->resources->frontController->baseUrl)) {
            $url = Zend_Registry::get('config')->resources->frontController->baseUrl;
        }
        else {
            $url = "/";
        }

        $this->view->url = $url;

        $sitemap = null;
        $Name = 'Sitemap.xml';
        $fileName = "../public/" . $Name;

        $sitemap = $sitemap . '<urlset>';

        foreach ($news as $n):


            $sitemap = $sitemap . '<url>';
            $sitemap = $sitemap . ' <loc>' . $url . "page/" . $n['id'] . '</loc>';
            $sitemap = $sitemap . ' <lastmod>' . date_format(new \DateTime("now"), "y.m.d") . '</lastmod>';
            $sitemap = $sitemap . '<changefreq>daily</changefreq>';
            $sitemap = $sitemap . '</url>';

        endforeach;

        $sitemap = $sitemap . ' </urlset>';
        $file = fopen($fileName, "w+");
        fwrite($file, $sitemap);
        fclose($file);

        $robottext = "Sitemap: " . $url . $Name;
        $robotfileaddres = "../public/robots.txt";
        $robotfile = fopen($robotfileaddres, "w+");
        fwrite($robotfile, $robottext);
        fclose($robotfile);

        $this->view->sitemap = $Name . " content : <br>" . $sitemap;
        $this->view->robot = $robottext . " rewrited";
    }

    public function sitemapAction()
    {
        $this->_helper->layout()->setLayout('new-kids-layout');

        $localeSession = new Zend_Session_Namespace('locale');
        $lang = $localeSession->locale;
        $this->view->locale = $lang;
        $localesManager = new \Managers\Locales();
        $locale = $localesManager->getByLocaleName($lang);

        // [1] Main menu.
        $pagesManager = new \Managers\Pages();
        $menu = $pagesManager->getMenu($locale['id']);

        $this->view->menu = $menu;
        $localeSession = new Zend_Session_Namespace('locale');
        $lang = $localeSession->locale;
        $pages = new \Managers\Locales();
        $menu = $pages->getByLocale($lang);

        $this->view->sitemap = $menu;


    }
}